package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.common.GetSessionId;
import com.itheima.common.Result;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    //    请求 URL: http://localhost:8080/shoppingCart/add
//    请求方法: POST
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCart shoppingCart) {

        //1.获取前端传来的数据
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        Long UserId = GetSessionId.GetId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, UserId);
        lqw.eq(dishId == null, ShoppingCart::getSetmealId, setmealId);
        lqw.eq(setmealId == null, ShoppingCart::getDishId, dishId);
        ShoppingCart one = shoppingCartService.getOne(lqw);

        if (one == null) {
            shoppingCart.setUserId(UserId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        } else {
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }

        return Result.success("添加成功");

    }

//    请求 URL: http://localhost:8080/shoppingCart/list
//    请求方法: GET

    @GetMapping("/list")
    public Result lists() {

        Long id = GetSessionId.GetId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, id);
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return Result.success(list);
    }

//    请求 URL: http://localhost:8080/shoppingCart/clean
//    请求方法: DELETE

    @DeleteMapping("/clean")
    public Result cleans() {

        Long id = GetSessionId.GetId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, id);
        shoppingCartService.remove(lqw);

        return Result.success("删除成功");
    }

    //请求 URL: http://localhost:8080/shoppingCart/sub
    //请求方法: POST
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCart shoppingCart) {

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();


        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        lqw.eq(dishId==null,ShoppingCart::getSetmealId, setmealId);
        lqw.eq(setmealId==null,ShoppingCart::getDishId,dishId);
        ShoppingCart one = shoppingCartService.getOne(lqw);


        int num = one.getNumber() - 1;

        if(num>=1){
            one.setNumber(num);
            shoppingCartService.updateById(one);
        }else{
            shoppingCartService.removeById(one);
        }

        return Result.success("修改购物车成功");
    }



}
